/* ========================================================================
 * Copyright 2007 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless reqired by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================================
 * Created on 11 oct. 2007
 * @author pollux
 */
package org.poxd.frisar.ui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.GeneralPath;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import org.poxd.frisar.Frisar;

public class DefaultHeadComponent extends JComponent {

    private static final long serialVersionUID = 42L;
    private final int MAX_WIDTH = 100;
    private final int MIN_WIDTH = 30;
    private final Stroke ROUNDED = new BasicStroke(2, BasicStroke.CAP_BUTT,
            BasicStroke.JOIN_ROUND);
    private Frisar frise;
    private int height;
    private int width = 60;
    private Paint bg;

    public DefaultHeadComponent(Frisar f) {
        frise = f;
        height = frise.getHeight();
        //setBorder(BorderFactory.createLineBorder(Color.RED));
    }

    @Override
    public void paintComponents(Graphics g) {
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setStroke(ROUNDED);
        int x = 0;
        int[] xPoints = {x, width / 2, width / 2, (int) (1.5 * width), width / 2, width / 2, x};
        height = frise.getHeight();
        int q = height / 4;
        int[] yPoints = {q, q, 0, (int) (2 * q), 4 * q, 3 * q, 3 * q};
        GeneralPath arrow = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xPoints.length);
        arrow.moveTo(xPoints[0], yPoints[0]);
        for (int index = 1; index < xPoints.length; index++) {
            arrow.lineTo(xPoints[index], yPoints[index]);
        }

        GradientPaint p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0,
                height - 1, new Color(100, 100, 100));
        bg = Color.LIGHT_GRAY;
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHints(rh);
        g2.setPaint(p1);
        g2.draw(arrow);
        g2.setPaint(bg);
        g2.fill(arrow);
    }

    public void setWidth(int width) {
        if (this.width != width) {
            if (width > MIN_WIDTH && width < MAX_WIDTH) {
                firePropertyChange("width", this.width, width);
                this.width = width;
            }
        }
    }

    @Override
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

    @Override
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension((int) Math.ceil(1.5 * width), height);
    }

    @Override
    public void setMaximumSize(Dimension maximumSize) {
    }

    @Override
    public void setMinimumSize(Dimension minimumSize) {
    }

    @Override
    public void setPreferredSize(Dimension preferredSize) {
    }
}